<template>
  <div class="content">
    <el-row type='flex' justify='space-between' align='middle' class='searchBox'>
      <el-col :span="20">
        <el-row type='flex' justify='start' align='middle'>
          <el-col :span='6'>
            企业名称:
            <el-input
              size='medium'
              style='width: 200px'
              v-model='companyModel.name'
              placeholder='企业名称'
              clearable
            />
          </el-col>

          <el-col :span='6'>
            污染源名称：
            <el-select size='medium' v-model="companyModel.wasteType" @change="search" placeholder="请选择">
              <el-option v-for="item in opWastes" :key="item.value" :label="item.label" :value="item.value" >{{item.label}}</el-option>
            </el-select>
          </el-col>
        </el-row>
      </el-col>
      <el-col :span="4">
        <el-button type='primary' @click='search'>搜索</el-button>
        <el-button type='success' @click='add'>新增</el-button>
        <el-button type='danger' @click='delBatch'>批量删除</el-button>
      </el-col>
    </el-row>
    <!-- table -->

    <el-card>
      <el-table class="tableForm" ref='multipleTable' :data="companyData" v-loading="companyTableloading" border @selection-change='handleSelectionChange'>
        <el-table-column type='selection' width='40'></el-table-column>
        <el-table-column width='50' align='center' type="index" label="序号" :index="indexMethod"></el-table-column>

        <el-table-column prop="name" label="企业名称1" align="center" width="300">
          <template slot-scope="scope">
            <el-link type="primary" @click="edit(scope.row)">{{scope.row.name}}</el-link>
          </template>
        </el-table-column>

        <el-table-column prop="type" label="企业类别" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.type == 1? "一般企业": "重点企业" }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="type" label="废气" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.vocsStr || "--" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="废水" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.waterStr || "--" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="危废" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.wasteStr || "--" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="固废" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.solidStr || "--" }}</span>
          </template>
        </el-table-column>


        <el-table-column prop="envPerson" label="环保负责人" align="center"></el-table-column>

        <el-table-column prop="envPersonPhone" label="环保负责人电话" align="center"></el-table-column>

        <el-table-column prop="industry" label="国民经济行业代码" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.industry.replaceAll(',', '/') }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="industryText" label="国民经济行业类别" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.industryText.split('/')[scope.row.industryText.split('/').length - 1] }}</span>
          </template>
        </el-table-column>

        <!-- <el-table-column
          prop="createdDate"
          label="创建时间"
          align="center"></el-table-column> -->

        <!-- <el-table-column fixed="right" label="操作" align="center">
          <template slot-scope="scope">
            <el-button @click="editUser(scope.row)" type="text" size="mini">编辑</el-button>
            <el-button @click="del(scope.row)" type="text" size="mini">删除</el-button>
          </template>
        </el-table-column> -->
      </el-table>

      <!-- 分页 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="companyModel.current"
      :page-sizes="[10, 20, 30]"
      :page-size="companyModel.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="companyModel.total"
    ></el-pagination>
    </el-card>
    <!-- 添加用户弹框 -->
    <addCompany v-if="addCompanyVisible"></addCompany>

    <!-- 编辑弹框 -->
    <editCompany v-if="editVisible" :editItem="editItem"></editCompany>
  </div>
</template>
<script>
import addCompany from "./addCompany";
import editCompany from "./editCompany";

export default {
  components: {
    addCompany, editCompany
  },
  data() {
    return {
      companyData: [],
      delList: [],
      companyTableloading: false,
      opWastes: [{
          value: '',
          label: '全部'
        }, {
          value: 'VOCS',
          label: '废气'
        }, {
          value: 'WATER',
          label: '废水'
        }, {
          value: 'SOLID',
          label: '一般固废'
        }, {
          value: 'WASTE',
          label: '危险废物'
        }],
      companyModel: {
        name: "",
        wasteType: "",

        current: 1,
        size: 10,
        total: 0,
      },
      //添加用户
      addCompanyVisible: false,
      //编辑用户
      editVisible: false,
      editItem: {},
    };
  },
  created() {
    this.search();
  },
  mounted() {
    this.$ge.$on("biz-company-change-search", () => {
      this.addCompanyVisible = false;
      this.editVisible = false;
      this.search();
    });
  },
  methods: {
    // 分页 ( 当页数量改变 )
    handleSizeChange(val) {
      this.companyModel.size = val;
      this.search();
    },
    // 分页 （ 页码改变 ）
    handleCurrentChange(val) {
      this.companyModel.current = val;
      this.search();
    },
    // 批量删除
    handleSelectionChange(val) {
      this.delList = val;
    },
    //查询企业列表
    search() {
      this.companyTableloading = true;
      this.$get("/biz-company/pageList", this.companyModel)
        .then((result) => {
          if (result.code == 200 && result.data) {
            this.companyData = result.data.records;

            this.companyModel.size = result.data.size;
            this.companyModel.total = result.data.total;
            this.companyModel.current = result.data.current;
          }
          this.companyTableloading = false;
        })
        .catch((error) => {
          //异常暂定为空
          this.companyData = [];
          this.companyTableloading = false;
        });
    },

    //添加
    add() {
      this.addCompanyVisible = true;
    },
    //编辑用户
    edit(row) {
      this.editVisible = true;
      this.editItem = row;
    },
    //删除用户
    delBatch() {
      if (this.delList.length == 0) {
        this.$message.warning('请勾选列表左侧要删除的数据')
        return;
      }
      let ids = this.delList.map(item => item.id)
      this.$confirm(
        '删除后无法恢复是否确认删除？',
        '删除提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        },
      )
        .then(() => {
          this.$postJson('/biz-company/delBatch', ids)
            .then((result) => {
              if (result.code == 200) {
                this.$message.success(result.msg);
                this.$refs.multipleTable.clearSelection();
                this.search();
              } else this.$message.error(result.msg);
            })
            .catch((error) => {
              //删除异常
              this.$message.error('删除异常');
            });
        })
        .catch(() => {
          //取消事件
        });
    },

  },
};
</script>
<style lang='scss' scoped>
.el-select {
  width: 70%;
}
</style>
